package com.app.service.impl;

import com.app.mapper.TypeMapper;
import com.app.model.*;
import com.app.service.TypeService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class TypeServiceImpl implements TypeService {
    @Autowired
    TypeMapper typeMapper;

    @Override
    public TypeModel getTypeByTypeId(int id) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        TypeModel typeModel = typeMapper.selectOne(queryWrapper);
        if (typeModel == null) {
            return null;
        } else {
            return typeModel;
        }
    }

    @Override
    public Page<TypeModel> getPageTypeList(Integer page, Integer limit, String name, Integer departmentId, List<Integer> idList, Integer sortType) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        if (idList == null || idList.size() <= 0) {
            queryWrapper.select();
        } else {
            queryWrapper.select().in("department_id", idList);
        }
        if (departmentId != null) {
            queryWrapper.eq("department_id", departmentId);
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        if (sortType==null || sortType==1){
            queryWrapper.orderByAsc("number").orderByDesc("create_time");
        }else {
            queryWrapper.orderByDesc("number").orderByDesc("create_time");
        }
        Page<TypeModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<TypeModel> typeList = typeMapper.selectPage(pages, queryWrapper);
        return typeList;
    }

    @Override
    public List<TypeModel> getTypeList(List<Integer> idList) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        if (idList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                queryWrapper.select().in("department_id", idList);
            }
        }
        queryWrapper.orderByDesc("id");
        List<TypeModel> typeModelList = typeMapper.selectList(queryWrapper);
        return typeModelList;
    }


    @Override
    public void update(TypeModel model) {
        typeMapper.updateById(model);
    }

    @Override
    public void insert(TypeModel model) {
        typeMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        typeMapper.deleteById(id);
    }

    @Override
    public Integer typeCount(List<Integer> idsList) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (idsList != null && idsList.size() > 0) {
            queryWrapper.in("department_id", idsList);
        }
        Integer typeCount = typeMapper.selectCount(queryWrapper);
        return typeCount;
    }

    @Override
    public List<TypeModel> typeListByDepId(List<Integer> idsList) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (idsList == null && idsList.size() <= 0) {
            return null;
        } else {
            for (int i = 0; i < idsList.size(); i++) {
                queryWrapper.or().eq("department_id", idsList.get(i));
            }
        }
        List<TypeModel> typeModelList = typeMapper.selectList(queryWrapper);
        return typeModelList;
    }

    @Override
    public List<TypeModel> getPlantByIds(List<Integer> appIdList) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().in("id", appIdList);
        List<TypeModel> personModelList = typeMapper.selectList(queryWrapper);
        return personModelList;
    }

    @Override
    public TypeModel selectByName(String modelTypeText) {
        QueryWrapper<TypeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", modelTypeText);
        return typeMapper.selectOne(queryWrapper);
    }
    /**
     * 批量删除type数据
     * @param ids
     * @return
     */
    @Override
    public Result<Object> deleteTypeBatchByIds(String ids) {
        // 参数校验
        if (StringUtils.isEmpty(ids)) {
            return Result.success("参数为空，无需删除");
        }
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        // 批量删除
        if (!ListUtils.isEmpty(idsList)) {
            typeMapper.deleteBatchIds(idsList);
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败，数据为空");
        }
    }
}
